package handlers

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"strconv"
	"time"
	"vehi-info-sys/cache"
	"vehi-info-sys/models"
)

func CarAdd(c *gin.Context) {
	car_node := c.PostForm("car_code")
	car_type := c.PostForm("car_type")
	car_price, _ := strconv.Atoi(c.PostForm("car_price"))

	data := models.Car{
		CarCode:  car_node,
		CarType:  car_type,
		CarPrice: car_price,
	}
	models.DB.Create(&data)

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}

func OrderAdd(c *gin.Context) {
	order_no := c.PostForm("order_no")
	order_name := c.PostForm("order_name")
	order_price, _ := strconv.Atoi(c.PostForm("order_price"))
	cid, _ := strconv.Atoi(c.PostForm("cid"))

	data := models.Order{
		OrderNo:    order_no,
		OrderName:  order_name,
		OrderPrice: order_price,
		Cid:        cid,
	}
	models.DB.Create(&data)

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}

func OrderList(c *gin.Context) {
	page, _ := strconv.Atoi(c.Query("page"))
	size, _ := strconv.Atoi(c.Query("size"))

	Offset := (page - 1) * size

	var orderList []models.OrderList

	/*
		SELECT
		  orders.id as OrderId,
		  cars.car_code as CarCode,
		  cars.car_type as CarType,
		  cars.car_price as CarPrice,
		  orders.order_no as OrderNo,
		  orders.order_name as OrderName,
		  orders.order_price as OrderPrice
		FROM
		  cars
		  LEFT JOIN orders ON cars.id = orders.cid
	*/
	redis_key := "order_list_page_" + strconv.Itoa(page)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.
			Table("cars").
			Select(
				"orders.id as OrderId",
				"cars.car_code as CarCode",
				"cars.car_type as CarType",
				"cars.car_price as CarPrice",
				"orders.order_no as OrderNo",
				"orders.order_name as OrderName",
				"orders.order_price as OrderPrice").
			Joins("LEFT JOIN orders ON cars.id = orders.cid").
			Offset(Offset).
			Limit(size).
			Debug().
			Find(&orderList)

		jsonData, _ := json.Marshal(orderList)

		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &orderList)
	}

	var total int64
	models.DB.Model(&models.Order{}).Count(&total)

	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "success",
		"data":  orderList,
		"total": total,
	})
	return
}

func OrderDetail(c *gin.Context) {
	id := c.Query("id")

	/*
		SELECT
		  orders.id as OrderId,
		  cars.car_code as CarCode,
		  cars.car_type as CarType,
		  cars.car_price as CarPrice,
		  orders.order_no as OrderNo,
		  orders.order_name as OrderName,
		  orders.order_price as OrderPrice
		FROM
		  cars
		  LEFT JOIN orders ON cars.id = orders.cid
		WHERE orders.id=1
	*/
	var orderList models.OrderList

	redis_key := "order_detail_" + id
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.
			Table("cars").
			Select(
				"orders.id as OrderId",
				"cars.car_code as CarCode",
				"cars.car_type as CarType",
				"cars.car_price as CarPrice",
				"orders.order_no as OrderNo",
				"orders.order_name as OrderName",
				"orders.order_price as OrderPrice").
			Joins("LEFT JOIN orders ON cars.id = orders.cid").
			Where("orders.id=?", id).
			Debug().
			Find(&orderList)

		jsonData, _ := json.Marshal(orderList)

		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &orderList)
	}

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "success",
		"data": orderList,
	})
	return
}
